package com.yequan.hbb.view.fragment.loginfragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.yequan.hbb.R;
import com.yequan.hbb.app.MyApplication;
import com.yequan.hbb.customview.Code;
import com.yequan.hbb.view.LoginActivity;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import static com.yequan.hbb.view.fragment.loginfragment.CheckFragment.handler;

/**
 * A simple {@link Fragment} subclass.
 */
public class RegisterFragment extends Fragment implements View.OnClickListener {

    private TextInputEditText phone;
    private TextInputEditText localCode;
    private ImageView randomCode;
    private Button getCode;
    private static final int CODE_ING = 1;   //已发送，倒计时
    private static final int CODE_REPEAT = 2;  //重新发送
    private static final int SMSDDK_HANDLER = 3;  //短信回调
    private int TIME = 60;//倒计时60s
    public final static int REGISTER = 1;
    private EventHandler eventHandler;
    private String userPhone;
    private String userLocalCode;
    private String createCode;

    public RegisterFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_register, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        phone = (TextInputEditText) view.findViewById(R.id.phone_input);
        localCode = (TextInputEditText) view.findViewById(R.id.code_input);
        randomCode = (ImageView) view.findViewById(R.id.random_code);
        getCode = (Button) view.findViewById(R.id.register_getcode);
        phone.setHint("请输入手机号");
        localCode.setHint("请输入验证码");
        randomCode.setImageBitmap(Code.getInstance().createBitmap());
        createCode = Code.getInstance().getCode().toLowerCase();
        MyApplication.level = REGISTER;
        initSDK();
        getCode.setOnClickListener(this);
        randomCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_getcode:
                userPhone = phone.getText().toString().trim();
                userLocalCode = localCode.getText().toString().trim();
                if ("".equals(userPhone) || "".equals(userLocalCode)) {
                    if ("".equals(userPhone)) {
                        phone.setError("手机号码不能为空");
                    }
                    if ("".equals(userLocalCode)) {
                        localCode.setError("验证码不能为空");
                    }
                } else {
                    if (!userLocalCode.equals(createCode)) {
                        localCode.setError("验证码错误");
                    } else {
                        SMSSDK.getVerificationCode("86", userPhone);
                        Intent intent = new Intent();
                        intent.setAction("com.yequan.orderbroadcast");
                        intent.putExtra("userPhone", userPhone);
                        getContext().sendOrderedBroadcast(intent, null);
                        LoginActivity.loginTitle.setText("验证");
                        getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.login_container, LoginActivity.checkFragment).commit();

//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    for (int i = 60; i > 0; i--) {
//                        handler.sendEmptyMessage(CODE_ING);
//                        if (i <= 0) {
//                            break;
//                        }
//                        try {
//                            Thread.sleep(1000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                    handler.sendEmptyMessage(CODE_REPEAT);
//                }
//            }).start();
                    }
                }
                break;
            case R.id.random_code:
                randomCode.setImageBitmap(Code.getInstance().createBitmap());
                createCode = Code.getInstance().getCode().toLowerCase();
                break;
        }

    }

    //初始化SMSSDK
    private void initSDK() {
        SMSSDK.initSDK(getContext(), "17e392901e066", "06be648b54c08a3781c12a4507de1700");
        eventHandler = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                Message msg = new Message();
                msg.arg1 = event;
                msg.arg2 = result;
                msg.obj = data;
                msg.what = SMSDDK_HANDLER;
                handler.sendMessage(msg);
            }
        };
        // 注册回调监听接口
        SMSSDK.registerEventHandler(eventHandler);
    }
}
